package com.wondertek.poms.search.controller;

import com.wondertek.oes.sso.client.service.sso.SsoRemoteService;
import com.wondertek.oes.sso.common.model.resp.Info;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.dao.po.es.BcHistoryVo;
import com.wondertek.poms.search.service.IBcHistoryVoService;
import com.wondertek.poms.search.vo.BcHistoryESVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenjijiang
 * @description
 */
@RestController
@RequestMapping("bc-history-es")
public class BcHistoryESController {
	@Autowired
	private IBcHistoryVoService iBcHistoryVoService;
	@Autowired
	private SsoRemoteService ssoRemoteService;

	/**
	 * @param bcHistoryESVo 播控历史vo
	 * @param page          页码
	 * @param start         开始的第一条记录id
	 * @param rows          记录数
	 * @return 查询的结果
	 */
	@GetMapping("/list")
	public ResultBean list(BcHistoryESVo bcHistoryESVo,
	                       @RequestParam(defaultValue = "1") int page,
	                       @RequestParam(defaultValue = "0") int start,
	                       @RequestParam(defaultValue = "20") int rows) {
		String bcUserId = null;
		String bcName = bcHistoryESVo.getBcName();
		if (StringUtils.isNotBlank(bcName)) {
			Info allUserInfo = ssoRemoteService.getAllUserInfo();
			List<String> users = allUserInfo.getUsers();
			for (String user : users) {
				//字符串为 用户id=用户名
				String[] arr = user.split("=");
				if (StringUtils.isNotBlank(arr[1]) && bcName.equals(arr[1])) {
					bcUserId = arr[0];
				}
			}
		}
		Long count = iBcHistoryVoService.getCount(bcHistoryESVo.getPrdContId(), bcHistoryESVo.getConId(), bcHistoryESVo.getBcSide(), bcHistoryESVo.getBcStartTime(),
				bcHistoryESVo.getBcEndTime(), bcHistoryESVo.getBcStatus(), bcUserId, bcHistoryESVo.getFirstType(), bcHistoryESVo.getPrdInfoId());
		List<BcHistoryVo> bcHistoryVoList = iBcHistoryVoService.getBcsPage(start, rows, bcHistoryESVo.getPrdContId(), bcHistoryESVo.getConId(), bcHistoryESVo.getBcSide(), bcHistoryESVo.getBcStartTime(),
				bcHistoryESVo.getBcEndTime(), bcHistoryESVo.getBcStatus(), bcUserId, bcHistoryESVo.getFirstType(), bcHistoryESVo.getPrdInfoId());
		Map<String, Object> map = new HashMap<>();
		map.put("count", count);
		map.put("data", bcHistoryVoList);
		return ResultBean.ok(map);
	}

}
